package com.todo.auth.entity;

import cn.hutool.extra.servlet.ServletUtil;
import com.todo.common.common.enums.GenderEnum;
import com.todo.common.utils.ServletHolderUtil;
import com.todo.common.utils.ip.IpRegionUtil;
import eu.bitwalker.useragentutils.UserAgent;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Arrays;
import java.util.Collection;

/**
 * @author zhangwenxiang
 * @date 2024/4/7 14:14
 * @description
 */
@Data
public class UserAccountDetail implements UserDetails {


    private UserAccount userAccount;

    /**
     * 登录信息
     */
    protected final LoginInfo loginInfo = new LoginInfo();

    public UserAccountDetail(UserAccount userAccount) {
        this.userAccount = userAccount;
    }

    /**
     * 设置用户代理信息
     */
    public void fillLoginInfo() {
        UserAgent userAgent = UserAgent.parseUserAgentString(ServletHolderUtil.getRequest().getHeader("User-Agent"));
        String ip = ServletUtil.getClientIP(ServletHolderUtil.getRequest());

        this.getLoginInfo().setIpAddress(ip);
        this.getLoginInfo().setLocation(IpRegionUtil.getBriefLocationByIp(ip));
        this.getLoginInfo().setBrowser(userAgent.getBrowser().getName());
        this.getLoginInfo().setOperationSystem(userAgent.getOperatingSystem().getName());
        this.getLoginInfo().setLoginTime(System.currentTimeMillis());
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        //返回当前用户的权限
        return Arrays.asList(new SimpleGrantedAuthority("DEFAULT"));
    }

    @Override
    public String getPassword() {
        return userAccount.getPassword();
    }

    @Override
    public String getUsername() {
        return userAccount.getUserName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    public UserAccount getUserAccount() {
        return userAccount;
    }

    @Data
    public static class UserAccount {
        private Long userId;
        private String userName;
        private String password;
        private Integer source;
        /**
         * 昵称
         */
        private String nickName;
        /**
         * 性别
         * {@link GenderEnum}
         */
        private GenderEnum gender;

        /**
         * 头像
         */
        private String headImg;
        /**
         * 是否真实用户
         */
        private Integer isReal;
    }

    @Data
    public static class LoginInfo {

        /**
         * 登录IP地址
         */
        private String ipAddress;

        /**
         * 登录地点
         */
        private String location;

        /**
         * 浏览器类型
         */
        private String browser;

        /**
         * 操作系统
         */
        private String operationSystem;

        /**
         * 登录时间
         */
        private Long loginTime;

    }
}
